package com.hss01248.dialog.bottomsheet;

import com.hss01248.dialog.ResourceTable;
import com.hss01248.dialog.ScreenUtil;
import com.hss01248.dialog.Tool;
import com.hss01248.dialog.adapter.SuperLvAdapter;
import com.hss01248.dialog.adapter.SuperLvHolder;
import com.hss01248.dialog.adapter.SuperPagerAdapter;
import com.hss01248.dialog.adapter.SuperPagerHolder;
import com.hss01248.dialog.config.BottomSheetStyle;
import com.hss01248.dialog.config.ConfigBean;
import com.hss01248.dialog.config.DefaultConfig;
import ohos.agp.components.*;
import ohos.agp.components.Text.TextSizeType;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;

/**
 * Created by Administrator on 2017/12/9.
 */

public class BottomSheetHolder extends SuperLvHolder<ConfigBean> {

    private Text tvTitle;
    private Text tvBottom;

    public BottomSheetHolder(Context context) {
        super(context);
    }

    @Override
    protected void findViews() {
        tvTitle = (Text) rootView.findComponentById(ResourceTable.Id_tv_title);
        tvBottom = (Text) rootView.findComponentById(ResourceTable.Id_tv_bottom);
    }

    @Override
    protected int setLayoutRes() {
        return ResourceTable.Layout_bottomsheet_lv;
    }

    @Override
    public void assingDatasAndEvents(Context context, final ConfigBean bean) {

        setTitleAndBottomButtonStyle(bean);
        ComponentContainer adapterView = null;
        if (bean.type == DefaultConfig.TYPE_BOTTOM_SHEET_LIST){
            adapterView =  setListView(context,bean);
        }else if(bean.type == DefaultConfig.TYPE_BOTTOM_SHEET_GRID){
            adapterView =   setGridView(context,bean);
        }

        setMdBottomSheetDialogBehaviour(adapterView,context,bean);




    }

    private void setMdBottomSheetDialogBehaviour(ComponentContainer adapterView, Context context, final ConfigBean bean) {
        if(bean.hasBehaviour && bean.dialog instanceof CommonDialog){
            if(adapterView !=null){
                Tool.handleScrollInBottomSheetDialog(adapterView);
            }
        }
    }

    private ComponentContainer setGridView(Context context, ConfigBean bean) {
        if(bean.hasBehaviour ){
            return buildSimpleGridView(context,bean);
        }else if(bean.lvDatas.size()<= 2* bean.gridColumns){
            return buildSimpleGridView(context,bean);
        }else {
            return buildGridViewWithViewPager(context,bean);
        }
    }

    private ComponentContainer buildGridViewWithViewPager(Context context, final ConfigBean bean) {
        PageSlider viewPager = new PageSlider(context);
        BottomSheetStyle style = bean.bottomSheetStyle;
        int height = ScreenUtil.dip2px( style.iconSizeDp+style.txtMarginTopDp+style.txtSizeSp+5)*2+ScreenUtil.dip2px(style.gvItemMarginV);

        DirectionalLayout.LayoutConfig params = new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT, height);

        params.setMarginTop(ScreenUtil.dip2px(style.gvMarginTopDp));
        params.setMarginBottom(ScreenUtil.dip2px(style.gvMarginBottomDp));
        params.setMarginLeft(ScreenUtil.dip2px(style.gcMarginLRDp));
        params.setMarginRight(params.getMarginLeft());
        viewPager.setLayoutConfig(params);


        final List<List<BottomSheetBean>> datas = new ArrayList<>();
        List<BottomSheetBean> data = null;
        for (int i = 0; i < bean.lvDatas.size(); i++) {
            if(i / bean.gridColumns/2 == datas.size()){
                data = new ArrayList<>();
                datas.add(data);
                data.add(bean.lvDatas.get(i));
            }else {
                data.add(bean.lvDatas.get(i));
            }
        }
        SuperPagerAdapter pagerAdapter = new SuperPagerAdapter(bean.context) {

            @Override
            protected SuperPagerHolder generateNewHolder(Context context, ComponentContainer container, int position) {
                return new BottomSheetPagerHolder(context).setPageNum(position).setConfigBean(bean);
            }
        };
        pagerAdapter.addAll(datas);
        viewPager.setProvider(pagerAdapter);

        ((ComponentContainer)rootView).addComponent(viewPager,1);

        final BottomVpIndicatorHolder indicatorHolder = new BottomVpIndicatorHolder(context);
        indicatorHolder.assingDatasAndEvents(context,datas);
        indicatorHolder.onPageSelected(0);
        ((ComponentContainer)rootView).addComponent(indicatorHolder.mLinearLayout,2);

        viewPager.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int itemPos, float itemPosOffset, int itemPosOffsetPixels) {
            }
            @Override
            public void onPageSlideStateChanged(int state) {
            }

            @Override
            public void onPageChosen(int itemPos) {
                indicatorHolder.onPageSelected(itemPos);
            }
        });
        return null;
    }

    private ListContainer buildSimpleGridView(Context context, final ConfigBean bean) {
        ListContainer listView = new ListContainer(bean.context);
        TableLayoutManager tableLayoutManager = new TableLayoutManager();

        BottomSheetStyle bottomSheetStyle = bean.bottomSheetStyle;

        tableLayoutManager.setColumnCount(bean.gridColumns);
        listView.setLayoutManager(tableLayoutManager);
        if (bean.mAdapter == null){
            SuperLvAdapter adapter = new SuperLvAdapter(bean.context) {
                @Override
                protected SuperLvHolder generateNewHolder(Context context, int itemViewType) {
                    BsGvHolder holder = new BsGvHolder(context);
                    holder.setStyle(bean.bottomSheetStyle);
                    return holder;
                }
            };
            bean.mAdapter = adapter;
        }
        listView.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer parent, Component view, int position, long id) {
                BottomSheetBean sheetBean =  bean.lvDatas.get(position);
                bean.itemListener.onItemClick(sheetBean.text,position);
                Tool.dismiss(bean,true);
            }
        });

        bean.mAdapter.addAll(bean.lvDatas);
        listView.setItemProvider(bean.mAdapter);

        ((ComponentContainer)rootView).addComponent(listView,1);
        return listView;
    }

    private ListContainer setListView(Context context, final ConfigBean bean) {
        ListContainer listView = new ListContainer(context);
        listView.setWidth(MATCH_PARENT);
        listView.setHeight(1000);
        DirectionalLayoutManager directionalLayoutManager = new DirectionalLayoutManager();
        directionalLayoutManager.setOrientation(Component.VERTICAL);
        listView.setLayoutManager(directionalLayoutManager);

        if (bean.mAdapter == null){
            SuperLvAdapter adapter = new SuperLvAdapter(bean.context) {
                @Override
                protected SuperLvHolder generateNewHolder(Context context, int itemViewType) {
                    return new BsLvHolder(context);
                }
            };

            bean.mAdapter = adapter;
        }

        bean.mAdapter.addAll(bean.lvDatas);

        listView.setItemProvider(bean.mAdapter);

        listView.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer lc, Component view, int position, long id) {
                BottomSheetBean sheetBean =  bean.lvDatas.get(position);
                bean.itemListener.onItemClick(sheetBean.text,position);
                Tool.dismiss(bean,true);
            }
        });

        ((ComponentContainer)rootView).addComponent(listView,1);
        return listView;
    }

    private void setTitleAndBottomButtonStyle(final ConfigBean bean) {
        if (Tool.isEmpty(bean.title)){
            tvTitle.setVisibility(Component.HIDE);
        }else {
            tvTitle.setText(bean.title.toString());
            tvTitle.setVisibility(Component.VISIBLE);
        }
        if(!Tool.isEmpty(bean.bottomTxt)){
            tvBottom.setVisibility(Component.VISIBLE);
            tvBottom.setText(bean.bottomTxt.toString());
            tvBottom.setTextSize(bean.bottomSheetStyle.bottomTxtSizeSp, TextSizeType.FP);
            Tool.setTextColorById(tvBottom, bean.bottomSheetStyle.bottomTxtColor);
            tvBottom.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component v) {
                    if(bean.itemListener!=null){
                        bean.itemListener.onBottomBtnClick();
                    }
                    Tool.dismiss(bean);
                }
            });
        }else {
            tvBottom.setVisibility(Component.HIDE);
        }
    }


}
